package com.example.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.management.common.QueryPageParam;
import com.example.management.common.Result;
import com.example.management.entity.User;
import com.example.management.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    //查询
    @GetMapping("/list")
    public List<User> list() { return userService.list(); }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user) ? Result.success() : Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody User user) {return userService.updateById(user) ? Result.success() : Result.fail();}
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody User user) {
        return userService.updateById(user);
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public boolean saveOfMod(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }
    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        return userService.removeById(id) ? Result.success() : Result.fail();
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        List<User> list = userService.lambdaQuery()
                .eq(User::getNo, user.getNo())
                .eq(User::getPwd, user.getPwd()).list();
        return list.isEmpty() ? Result.fail() : Result.success(list.getFirst());
    }
    @GetMapping("/findByNo")
    public Result findByNo(@RequestParam String no) {
        List<User> list = userService.lambdaQuery().eq(User::getNo, no).list();
        return list.isEmpty() ? Result.fail() : Result.success();
    }
    //查询（模糊、匹配）
    @PostMapping("/listP")
    public Result listP(@RequestBody User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(User::getName, user.getName());
        return Result.success(userService.list(lambdaQueryWrapper));
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        System.out.println(query);
        System.out.println("num==" + query.getPageNum());
        System.out.println("size==" + query.getPageSize());
        HashMap param = query.getParam();
        String no = (String) param.get("no");
        String sex = (String) param.get("sex");

        Page<User> page = new Page<>(query.getPageNum(), query.getPageSize());
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(no) && !"null".equals(no)) {
            lambdaQueryWrapper.like(User::getNo, no);
        }
        if(StringUtils.isNotBlank(sex) && !"null".equals(sex)) {
            lambdaQueryWrapper.eq(User::getSex, sex);
        }

        IPage<User> data = userService.page(page, lambdaQueryWrapper);
        System.out.println("total==" + data.getTotal());

        return Result.success(data.getRecords(), data.getTotal());
    }
}